package com.tianchi.lexiang.util;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

import android.util.Log;

import com.tianchi.lexiang.background.AVHDMISetting;

public class AVHDMISettingUtil {
	private static final String TAG = "AVHDMISwitchActivity";
	
	public static void createNewFile(String fileName){
    	checkDir();
    	File des = new File(AVHDMISetting.PATH+"/"+fileName);
    	try {
    		if (!des.exists()) {
    			des.createNewFile();
    			OutputStream os = new FileOutputStream(des);
    			os.write(0);
    			os.close();
			}else {
				Log.d(TAG,"[createNewFile]" + AVHDMISetting.PATH+"/"+fileName + " is already exits");
			}
		} catch (IOException e) {
			Log.e(TAG, "[createNewFile] " + AVHDMISetting.PATH + "/" + fileName + "failed" , e);
			e.printStackTrace();
		}
    }
	
	public static void deleteOldFile(){
    	File dir = checkDir();
    	String[] files = dir.list();
    	File temp;
    	for(String file : files){
    		temp = new File(dir + File.separator + file);
    		if (temp.exists()) {
    			Log.d(TAG, "[deleteOldFile]will delete file = " + file);
				temp.delete();
			}else {
				Log.d(TAG, "[deleteOldFile]file " + file + " isn't exits , continue");
				continue;
			}
    	}
    }
	
    public static File checkDir(){
    	File dir = new File(AVHDMISetting.PATH);
    	if (!dir.exists()) {
    		Log.d(TAG, "[checkDir] " + AVHDMISetting.PATH + " is not exits");
			dir.mkdirs();
		}
    	return dir;
    }
}
